/**
 *  Template.java        1.00 15 Nov, 2009
 *  
 *  Copyright (c) 2008 XProtean, Inc.
 *  461 S. Milpitas Blvd, Milpitas, CA, 95035, U.S.A.
 *  All rights reserved.
 * 
 *  This software is the confidential and proprietary information of 
 *  XProtean, Inc. ("Confidential Information").  You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with XProtean Inc.
 *   
 *  gprasad      1.00    15 Nov, 2009     gprasad     Initial code
 *
 */
package org.ql.client.framework;

import com.google.gwt.user.client.ui.HTMLPanel;
import org.ql.client.framework.util.HTMLUtil;
import java.util.HashMap;
import java.util.Set;

/**
 *
 * @author gprasad
 */
public class Template implements Renderable {

    private String templateHtmlSrc = null;
    private String templateHtmlContent = null;
    private HashMap<String, Renderable> defaultRenderables = new HashMap<String, Renderable>();
    private HashMap<String, Renderable> currentRenderables = new HashMap<String, Renderable>();
    private HTMLPanel htmlPanel = null;
    private Template containerTemplate = null;
    private String containedAreaId = null;

    protected Template(String templateHtmlSrc) {

        this.templateHtmlSrc = templateHtmlSrc;
        this.templateHtmlContent = HTMLUtil.getHtmlContent(this.templateHtmlSrc);
    }

    public void setDefaultContent(String containerId, Renderable defaultRendererable) {

        defaultRenderables.put(containerId, defaultRendererable);
        currentRenderables.put(containerId, defaultRendererable);
    }

    @Override
    public void render(Template container, String containedAreaId) {

        this.containerTemplate = container;
        this.containedAreaId = containedAreaId;
        renderTemplate();
        renderKnownAreas();
    }

    public void replaceArea(String areaId, Renderable renderable) {
        Renderable existingRenderable = currentRenderables.get(areaId);
        if (existingRenderable != null && existingRenderable.getWidget() != null) {
            getWidget().remove(existingRenderable.getWidget());
        }
        currentRenderables.put(areaId, renderable);
        renderArea(containedAreaId, renderable);
    }

    private void renderArea(String containedArea, Renderable renderable) {

        getWidget().add(renderable.getWidget(), containedArea);
    }

    private Template getContainerTemplate() {

        return containerTemplate;
    }

    public HTMLPanel getWidget() {
        return htmlPanel;
    }

    private void renderTemplate() {
        if (this.htmlPanel != null) {
            this.htmlPanel.clear();
        }
        this.htmlPanel = new HTMLPanel(templateHtmlContent);
        getContainerTemplate().renderArea(containedAreaId, this);
    }

    private void renderKnownAreas() {

        Set<String> areaIds = currentRenderables.keySet();
        for (String areaId : areaIds) {
            renderArea(areaId, currentRenderables.get(areaId));
        }
    }
}
